package com.diandian.backend.store.service;

import com.diandian.backend.store.constants.DianDianStoreConstant;
import com.diandian.backend.store.dto.*;
import com.diandian.backend.store.resp.StoreJingdongHomeStoreInfoTreeListResp;
import com.diandian.backend.store.resp.StoreJingdongHomeStoreTreeDetailResp;
import com.diandian.backend.store.resp.StoreJingdongHomeStoreTreeListResp;
import com.diandian.backend.store.resp.StoreJingdongHomeStoreTreeResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class JingdongHomeStoreTreeService {

    @Autowired
    private StoreJingdongHomeStoreTreeService storeJingdongHomeStoreTreeService;

    @Autowired StoreJingdongHomeStoreInfoTreeService storeJingdongHomeStoreInfoTreeService;

    @Transactional(rollbackFor = Exception.class)
    public void addNode(StoreJingdongHomeStoreTreeAddParamDTO dataDTO){

        dataDTO.check();

        String belong = dataDTO.getBelong();
        String orgId = dataDTO.getOrgId();
        String orgCode = dataDTO.getOrgCode();
        String nodeCode = dataDTO.getNodeCode();
        String nodeName = dataDTO.getNodeName();
        String parentNodeCode = dataDTO.getParentNodeCode();

        // 校验新节点编码是否已经存在
        StoreJingdongHomeStoreTreeDetailParamDTO existsDTO = new StoreJingdongHomeStoreTreeDetailParamDTO();
        existsDTO.setBelong(belong);
        existsDTO.setOrgId(orgId);
        existsDTO.setOrgCode(orgCode);
        existsDTO.setNodeCode(nodeCode);
        existsDTO.setDel(0);
        StoreJingdongHomeStoreTreeDetailResp treeDetailResp =  storeJingdongHomeStoreTreeService.getDetail(existsDTO);
        if(treeDetailResp != null){
            throw new DefaultException("节点编码已存在");
        }

        // 校验新节点名称是否已经存在
        existsDTO = new StoreJingdongHomeStoreTreeDetailParamDTO();
        existsDTO.setBelong(belong);
        existsDTO.setOrgId(orgId);
        existsDTO.setOrgCode(orgCode);
        existsDTO.setNodeName(nodeName);
        existsDTO.setDel(0);
        treeDetailResp =  storeJingdongHomeStoreTreeService.getDetail(existsDTO);
        if(treeDetailResp != null){
            throw new DefaultException("节点名称已存在");
        }

        // 校验节点是否合法
        String parentNodePath;
        int nodeLevel;
        if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(parentNodeCode)){
            StoreJingdongHomeStoreTreeDetailParamDTO treeDetailParamDTO = new StoreJingdongHomeStoreTreeDetailParamDTO();
            treeDetailParamDTO.setNodeCode(parentNodeCode);
            treeDetailParamDTO.setBelong(belong);
            treeDetailParamDTO.setOrgId(orgId);
            treeDetailParamDTO.setOrgCode(orgCode);
            treeDetailParamDTO.setDel(0);
            StoreJingdongHomeStoreTreeDetailResp parentTreeDetailResp = storeJingdongHomeStoreTreeService.getDetail(treeDetailParamDTO);
            if(parentTreeDetailResp == null){
                throw new DefaultException("父级节点不合法");
            }
            nodeLevel = parentTreeDetailResp.getNodeLevel() + 1;
            parentNodePath = parentTreeDetailResp.getNodePath();
        } else {
            nodeLevel = 2;
            parentNodePath = DianDianStoreConstant.TREE_NODE_CODE_TOP;
        }

        // 添加节点信息
        StoreJingdongHomeStoreTreeAddParamDTO nodeAddParamDTO = new StoreJingdongHomeStoreTreeAddParamDTO();
        nodeAddParamDTO.setType(dataDTO.getType());
        nodeAddParamDTO.setNodeCode(dataDTO.getNodeCode());
        nodeAddParamDTO.setNodeName(dataDTO.getNodeName());
        nodeAddParamDTO.setNodeLevel(nodeLevel);
//        nodeAddParamDTO.setNodePath("");
        nodeAddParamDTO.setParentNodeCode(parentNodeCode);
        nodeAddParamDTO.setRemark(dataDTO.getRemark());
        nodeAddParamDTO.setBelong(belong);
        nodeAddParamDTO.setCreateUserId(dataDTO.getCreateUserId());
        nodeAddParamDTO.setCreateUserName(dataDTO.getCreateUserName());
        nodeAddParamDTO.setOrgId(orgId);
        nodeAddParamDTO.setOrgCode(orgCode);
        storeJingdongHomeStoreTreeService.add(nodeAddParamDTO);

        // 回写节点路径
        Long newNodeId = nodeAddParamDTO.getId();
        String newNodePath = parentNodePath + "." + newNodeId;
        storeJingdongHomeStoreTreeService.updateNodePath(newNodeId, newNodePath);

    }

    public StoreJingdongHomeStoreTreeDetailResp getDetail(StoreJingdongHomeStoreTreeDetailParamDTO paramDTO){
        return storeJingdongHomeStoreTreeService.getDetail(paramDTO);
    }

    /**
     * 仅更新节点基本信息，不处理节点上级节点信息
     * @param dataDTO
     */
    public void updateNode(StoreJingdongHomeStoreTreeUpdateByIdParamDTO dataDTO){

        dataDTO.check();
        Long id = dataDTO.getId();
        if(DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(String.valueOf(id)) || DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(String.valueOf(id))){
            throw new DefaultException("根节点和未分组节点不能操作");
        }

        String belong = dataDTO.getBelong();
        String orgId = dataDTO.getOrgId();
        String orgCode = dataDTO.getOrgCode();
        String nodeCode = dataDTO.getNodeCode();

        StoreJingdongHomeStoreTreeDetailParamDTO existsDTO = new StoreJingdongHomeStoreTreeDetailParamDTO();
        existsDTO.setBelong(belong);
        existsDTO.setOrgId(orgId);
        existsDTO.setOrgCode(orgCode);
        existsDTO.setNodeCode(nodeCode);
        existsDTO.setDel(0);
        StoreJingdongHomeStoreTreeDetailResp treeDetailResp =  storeJingdongHomeStoreTreeService.getDetail(existsDTO);
        if(treeDetailResp == null){
            throw new DefaultException("节点编码不存在");
        }

        storeJingdongHomeStoreTreeService.updateById(dataDTO);
    }

    /**
     * 删除节点（存在下级节点和存在关联门店的不能删除）
     * @param dataDTO
     */
    public void deleteNode(StoreJingdongHomeStoreTreeDeleteByIdParamDTO dataDTO){

        dataDTO.check();
        Long id = dataDTO.getId();

        if(DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(String.valueOf(id)) || DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(String.valueOf(id))){
            throw new DefaultException("根节点和未分组节点不能操作");
        }

        StoreJingdongHomeStoreTreeDetailParamDTO existsDTO = new StoreJingdongHomeStoreTreeDetailParamDTO();
        existsDTO.setId(id);
        StoreJingdongHomeStoreTreeDetailResp treeDetailResp =  storeJingdongHomeStoreTreeService.getDetail(existsDTO);
        if(treeDetailResp == null){
            throw new DefaultException("节点编码不存在");
        }

        // 有下级节点的不能删除
        StoreJingdongHomeStoreTreeListParamDTO listParamDTO = new StoreJingdongHomeStoreTreeListParamDTO();
        listParamDTO.setParentNodeCodeEql(treeDetailResp.getNodeCode());
        List<StoreJingdongHomeStoreTreeListResp> nextNodeList = storeJingdongHomeStoreTreeService.getList(listParamDTO);
        if(nextNodeList != null && nextNodeList.size() > 0){
            throw new DefaultException("存在下级节点不能删除");
        }

        // 关联门店的节点不能删除
        StoreJingdongHomeStoreInfoTreeListParamDTO infoTreeListParamDTO = new StoreJingdongHomeStoreInfoTreeListParamDTO();
        infoTreeListParamDTO.setBelongEql(treeDetailResp.getBelong());
        infoTreeListParamDTO.setOrgIdEql(treeDetailResp.getOrgId());
        infoTreeListParamDTO.setOrgCodeEql(treeDetailResp.getOrgCode());
        infoTreeListParamDTO.setNodeCodeEql(treeDetailResp.getNodeCode());
        List<StoreJingdongHomeStoreInfoTreeListResp> infoTreeListRespList = storeJingdongHomeStoreInfoTreeService.getList(infoTreeListParamDTO);
        if(infoTreeListRespList != null && infoTreeListRespList.size() > 0){
            throw new DefaultException("已关联门店，无法删除");
        }

        storeJingdongHomeStoreTreeService.deleteById(dataDTO);
    }

    public List<StoreJingdongHomeStoreTreeResp> queryMultiTree(StoreJingdongHomeStoreTreeQueryParamDTO dataDTO){

        dataDTO.check();

        // 构建顶级节点
        StoreJingdongHomeStoreTreeResp topNode = new StoreJingdongHomeStoreTreeResp();
        topNode.setId(Long.parseLong(DianDianStoreConstant.TREE_NODE_CODE_TOP));
        topNode.setNodeCode(DianDianStoreConstant.TREE_NODE_CODE_TOP);
        topNode.setNodeName(DianDianStoreConstant.TREE_NODE_NAME_TOP);
        topNode.setNodePath(DianDianStoreConstant.TREE_NODE_CODE_TOP);
        topNode.setParentNodeCode(DianDianStoreConstant.TREE_NODE_CODE_ALL);
        topNode.setChildren(new ArrayList<>());
        topNode.setNodeLevel(1);

        // 构建未分组节点
        StoreJingdongHomeStoreTreeResp unGroupNode = new StoreJingdongHomeStoreTreeResp();
        unGroupNode.setId(Long.parseLong(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP));
        unGroupNode.setNodeCode(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP);
        unGroupNode.setNodeName(DianDianStoreConstant.TREE_NODE_NAME_UNGROUP);
        unGroupNode.setNodePath(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP);
        topNode.setParentNodeCode(DianDianStoreConstant.TREE_NODE_CODE_TOP);
        unGroupNode.setChildren(new ArrayList<>());
        unGroupNode.setNodeLevel(2);

        // 按照level排序获取所有节点数据
        StoreJingdongHomeStoreTreeListParamDTO treeListParamDTO = new StoreJingdongHomeStoreTreeListParamDTO();
        treeListParamDTO.setBelongEql(dataDTO.getBelong());
        treeListParamDTO.setOrgIdEql(dataDTO.getOrgId());
        treeListParamDTO.setOrgCodeEql(dataDTO.getOrgCode());
        List<StoreJingdongHomeStoreTreeListResp> treeListResp = storeJingdongHomeStoreTreeService.getList(treeListParamDTO);
        if(treeListResp == null || treeListResp.size() == 0){
            topNode.setChildren(Arrays.asList(unGroupNode));
            return Arrays.asList(topNode);
        }

        // 格式转换
        List<StoreJingdongHomeStoreTreeResp> treeRespList = new ArrayList<>();
        for (int i = 0; i < treeListResp.size(); i++) {
            StoreJingdongHomeStoreTreeResp item = new StoreJingdongHomeStoreTreeResp();
            item.setId(treeListResp.get(i).getId());
            item.setType(treeListResp.get(i).getType());
            item.setNodePath(treeListResp.get(i).getNodePath());
            item.setRemark(treeListResp.get(i).getRemark());
            item.setBelong(treeListResp.get(i).getBelong());
            item.setCreateUserId(treeListResp.get(i).getCreateUserId());
            item.setCreateUserName(treeListResp.get(i).getCreateUserName());
            item.setCreateTime(treeListResp.get(i).getCreateTime());
            item.setOrgId(treeListResp.get(i).getOrgId());
            item.setOrgCode(treeListResp.get(i).getOrgCode());
            item.setUpdateUserId(treeListResp.get(i).getUpdateUserId());
            item.setUpdateUserName(treeListResp.get(i).getUpdateUserName());
            item.setUpdateTime(treeListResp.get(i).getUpdateTime());
            item.setNodeCode(treeListResp.get(i).getNodeCode());
            item.setNodeName(treeListResp.get(i).getNodeName());
            item.setNodeLevel(treeListResp.get(i).getNodeLevel());
            item.setParentNodeCode(treeListResp.get(i).getParentNodeCode());
            item.setRemark(treeListResp.get(i).getRemark());
            item.setChildren(new ArrayList<>());
            treeRespList.add(item);
        }

        // 按照level进行分组
        Map<String, List<StoreJingdongHomeStoreTreeResp>> tempMap = new LinkedHashMap();
        for (int i = 0; i < treeRespList.size(); i++) {
            StoreJingdongHomeStoreTreeResp item = treeRespList.get(i);
            String nodeLevel = String.valueOf(item.getNodeLevel());
            if(tempMap.get(nodeLevel) == null){
                List<StoreJingdongHomeStoreTreeResp> tempList = new ArrayList<>();
                tempList.add(item);
                tempMap.put(nodeLevel, tempList);
            } else {
                tempMap.get(nodeLevel).add(item);
            }
        }
        int batchCount = tempMap.size();

        // 如果只有一层时，无需进入递归，直接组装返回
        if(batchCount == 1){
            List<StoreJingdongHomeStoreTreeResp> childrenList = new ArrayList<>();
            childrenList.addAll(tempMap.get("2"));
            childrenList.add(unGroupNode);
            topNode.setChildren(childrenList);
            return Arrays.asList(topNode);
        }

        // 组装最终返回结果
        List<StoreJingdongHomeStoreTreeResp> childrenList = null;
        for (int i = batchCount + 1; i > 2 ; i--) {

            List<StoreJingdongHomeStoreTreeResp> innerList;
            List<StoreJingdongHomeStoreTreeResp> outerList;
            if(i == batchCount + 1){
                outerList = tempMap.get(String.valueOf(i-1));
                innerList = tempMap.get(String.valueOf(i));
            } else {
                outerList = tempMap.get(String.valueOf(i-1));
                innerList = childrenList;
            }
            childrenList = makeChildren(outerList, innerList);
        }

        // 返回
        childrenList.add(unGroupNode);
        topNode.setChildren(childrenList);
        return Arrays.asList(topNode);
    }

    public List<StoreJingdongHomeStoreTreeResp> queryTree(StoreJingdongHomeStoreTreeQueryParamDTO dataDTO){

        dataDTO.check();

        List<StoreJingdongHomeStoreTreeResp> resultList = new ArrayList<>();

        // 构建顶级节点
        StoreJingdongHomeStoreTreeResp topNode = new StoreJingdongHomeStoreTreeResp();
        topNode.setId(Long.parseLong(DianDianStoreConstant.TREE_NODE_CODE_TOP));
        topNode.setNodeCode(DianDianStoreConstant.TREE_NODE_CODE_TOP);
        topNode.setNodeName(DianDianStoreConstant.TREE_NODE_NAME_TOP);
        topNode.setNodePath(DianDianStoreConstant.TREE_NODE_CODE_TOP);
        topNode.setParentNodeCode(DianDianStoreConstant.TREE_NODE_CODE_ALL);
        topNode.setNodeLevel(1);
        topNode.setChildren(new ArrayList<>());
        resultList.add(topNode);

        // 构建未分组节点
        StoreJingdongHomeStoreTreeResp unGroupNode = new StoreJingdongHomeStoreTreeResp();
        unGroupNode.setId(Long.parseLong(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP));
        unGroupNode.setNodeCode(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP);
        unGroupNode.setNodeName(DianDianStoreConstant.TREE_NODE_NAME_UNGROUP);
        unGroupNode.setNodePath(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP);
        unGroupNode.setParentNodeCode(DianDianStoreConstant.TREE_NODE_CODE_TOP);
        unGroupNode.setChildren(new ArrayList<>());
        unGroupNode.setNodeLevel(2);

        // 按照level排序获取所有节点数据
        StoreJingdongHomeStoreTreeListParamDTO treeListParamDTO = new StoreJingdongHomeStoreTreeListParamDTO();
        treeListParamDTO.setBelongEql(dataDTO.getBelong());
        treeListParamDTO.setOrgIdEql(dataDTO.getOrgId());
        treeListParamDTO.setOrgCodeEql(dataDTO.getOrgCode());
        List<StoreJingdongHomeStoreTreeListResp> treeListResp = storeJingdongHomeStoreTreeService.getList(treeListParamDTO);
        if(treeListResp == null || treeListResp.size() == 0){
            resultList.add(unGroupNode);
            return resultList;
        }

        // 格式转换
        for (int i = 0; i < treeListResp.size(); i++) {
            StoreJingdongHomeStoreTreeResp item = new StoreJingdongHomeStoreTreeResp();
            item.setId(treeListResp.get(i).getId());
            item.setType(treeListResp.get(i).getType());
            item.setNodePath(treeListResp.get(i).getNodePath());
            item.setRemark(treeListResp.get(i).getRemark());
            item.setBelong(treeListResp.get(i).getBelong());
            item.setCreateUserId(treeListResp.get(i).getCreateUserId());
            item.setCreateUserName(treeListResp.get(i).getCreateUserName());
            item.setCreateTime(treeListResp.get(i).getCreateTime());
            item.setOrgId(treeListResp.get(i).getOrgId());
            item.setOrgCode(treeListResp.get(i).getOrgCode());
            item.setUpdateUserId(treeListResp.get(i).getUpdateUserId());
            item.setUpdateUserName(treeListResp.get(i).getUpdateUserName());
            item.setUpdateTime(treeListResp.get(i).getUpdateTime());
            item.setNodeCode(treeListResp.get(i).getNodeCode());
            item.setNodeName(treeListResp.get(i).getNodeName());
            item.setNodeLevel(treeListResp.get(i).getNodeLevel());
            item.setParentNodeCode(treeListResp.get(i).getParentNodeCode());
            item.setRemark(treeListResp.get(i).getRemark());
            item.setChildren(new ArrayList<>());
            resultList.add(item);
        }

        resultList.add(unGroupNode);
        return resultList;
    }

    private List<StoreJingdongHomeStoreTreeResp> makeChildren(List<StoreJingdongHomeStoreTreeResp> parentList, List<StoreJingdongHomeStoreTreeResp> childList){

        if(childList == null || childList.size() == 0){
            return parentList;
        }

        List<StoreJingdongHomeStoreTreeResp> resultList = new ArrayList<>();

        for (int j = 0; j < parentList.size(); j++) {

            StoreJingdongHomeStoreTreeResp jChildren = new StoreJingdongHomeStoreTreeResp();
            jChildren.setId(parentList.get(j).getId());
            jChildren.setType(parentList.get(j).getType());
            jChildren.setRemark(parentList.get(j).getRemark());
            jChildren.setBelong(parentList.get(j).getBelong());
            jChildren.setCreateUserId(parentList.get(j).getCreateUserId());
            jChildren.setCreateUserName(parentList.get(j).getCreateUserName());
            jChildren.setCreateTime(parentList.get(j).getCreateTime());
            jChildren.setOrgId(parentList.get(j).getOrgId());
            jChildren.setOrgCode(parentList.get(j).getOrgCode());
            jChildren.setUpdateUserId(parentList.get(j).getUpdateUserId());
            jChildren.setUpdateUserName(parentList.get(j).getUpdateUserName());
            jChildren.setUpdateTime(parentList.get(j).getUpdateTime());
            jChildren.setNodeCode(parentList.get(j).getNodeCode());
            jChildren.setNodeName(parentList.get(j).getNodeName());
            jChildren.setNodePath(parentList.get(j).getNodePath());
            jChildren.setNodeLevel(parentList.get(j).getNodeLevel());
            jChildren.setParentNodeCode(parentList.get(j).getParentNodeCode());

            List<StoreJingdongHomeStoreTreeResp> jChildrenList = new ArrayList<>();
            String nodeCode = parentList.get(j).getNodeCode();
            for (int k = 0; k < childList.size(); k++) {
                String parentNodeCode = childList.get(k).getParentNodeCode();
                if(parentNodeCode.equals(nodeCode)){
                    StoreJingdongHomeStoreTreeResp kChildren = new StoreJingdongHomeStoreTreeResp();
                    kChildren.setId(childList.get(k).getId());
                    kChildren.setNodePath(childList.get(k).getNodePath());
                    kChildren.setRemark(childList.get(k).getRemark());
                    kChildren.setBelong(childList.get(k).getBelong());
                    kChildren.setCreateUserId(childList.get(k).getCreateUserId());
                    kChildren.setCreateTime(childList.get(k).getCreateTime());
                    kChildren.setCreateUserName(childList.get(k).getCreateUserName());
                    kChildren.setUpdateUserId(childList.get(k).getUpdateUserId());
                    kChildren.setUpdateTime(childList.get(k).getUpdateTime());
                    kChildren.setUpdateUserName(childList.get(k).getUpdateUserName());
                    kChildren.setOrgId(childList.get(k).getOrgId());
                    kChildren.setOrgCode(childList.get(k).getOrgCode());
                    kChildren.setNodeCode(childList.get(k).getNodeCode());
                    kChildren.setNodeName(childList.get(k).getNodeName());
                    kChildren.setNodePath(childList.get(k).getNodePath());
                    kChildren.setNodeLevel(childList.get(k).getNodeLevel());
                    kChildren.setParentNodeCode(childList.get(k).getParentNodeCode());
                    kChildren.setChildren(childList.get(k).getChildren() == null ? new ArrayList() : childList.get(k).getChildren());
                    jChildrenList.add(kChildren);
                }
            }
            jChildren.setChildren(jChildrenList);
            resultList.add(jChildren);
        }

        return resultList;
    }

    @Transactional(rollbackFor = Exception.class)
    public void adjustNode(Long id, String targetParentCode){

        if(DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(String.valueOf(id)) || DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(String.valueOf(id))){
            throw new DefaultException("根节点和未分组节点不能操作");
        }
        if(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(targetParentCode)){
            throw new DefaultException("不能移动到未分组下");
        }

        // 校验当前节点是否存在
        StoreJingdongHomeStoreTreeDetailParamDTO existsDTO = new StoreJingdongHomeStoreTreeDetailParamDTO();
        existsDTO.setId(id);
        StoreJingdongHomeStoreTreeDetailResp treeDetailResp =  storeJingdongHomeStoreTreeService.getDetail(existsDTO);
        if(treeDetailResp == null){
            throw new DefaultException("节点不存在");
        }
        String currentNodeCode = treeDetailResp.getNodeCode();
        Long currentNodeId = treeDetailResp.getId();
        String belong = treeDetailResp.getBelong();
        String currentNodePath = treeDetailResp.getNodePath();
        String orgId = treeDetailResp.getOrgId();
        String orgCode = treeDetailResp.getOrgCode();

        // 源节点不能与目标节点相同
        if(currentNodeCode.equals(targetParentCode)){
            throw new DefaultException("不能移动到当前节点下");
        }

        // 校验目标父节点是否存在
        String parentNodeCode;
        String parentNodePath;
        int nodeLevel;
        if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(targetParentCode)){
            existsDTO = new StoreJingdongHomeStoreTreeDetailParamDTO();
            existsDTO.setBelong(belong);
            existsDTO.setOrgId(treeDetailResp.getOrgId());
            existsDTO.setOrgCode(treeDetailResp.getOrgCode());
            existsDTO.setNodeCode(targetParentCode);
            treeDetailResp =  storeJingdongHomeStoreTreeService.getDetail(existsDTO);
            if(treeDetailResp == null){
                throw new DefaultException("目标节点不存在");
            }
            parentNodeCode = treeDetailResp.getNodeCode();
            parentNodePath = treeDetailResp.getNodePath();
            nodeLevel = treeDetailResp.getNodeLevel() + 1;
        } else {
            parentNodeCode = DianDianStoreConstant.TREE_NODE_CODE_TOP;
            parentNodePath = DianDianStoreConstant.TREE_NODE_CODE_TOP;
            nodeLevel = 2;
        }

        // 调整当前节点：path和父节点编码
        String targetNodePath = parentNodePath + "." + currentNodeId;
        storeJingdongHomeStoreTreeService.updateParentInfo(id, parentNodeCode, targetNodePath, nodeLevel);

        // 调整当前节点的下级节点的path值
        StoreJingdongHomeStoreTreeListParamDTO listParamDTO = new StoreJingdongHomeStoreTreeListParamDTO();
        listParamDTO.setBelongEql(belong);
        listParamDTO.setOrgIdEql(orgId);
        listParamDTO.setOrgCodeEql(orgCode);
        listParamDTO.setNodePathPrefix(currentNodePath);
        List<StoreJingdongHomeStoreTreeListResp> nextNodeList = storeJingdongHomeStoreTreeService.getList(listParamDTO);
        if(nextNodeList != null && nextNodeList.size() > 0){

            List<StoreJingdongHomeStoreTreeUpdateByIdParamDTO> nextNodeUpdateList = new ArrayList<>();
            for (int i = 0; i < nextNodeList.size(); i++) {
                Long itemId = nextNodeList.get(i).getId();
                String itemNode = nextNodeList.get(i).getNodeCode();
                String itemNodePath = nextNodeList.get(i).getNodePath();
                if(itemNode.equals(currentNodeCode)){
                    continue;
                }
                String itemTargetNodePath = itemNodePath.replaceFirst(currentNodePath, targetNodePath);
                String itemTargetNodeLevel = String.valueOf(itemTargetNodePath.split("\\.").length);

                StoreJingdongHomeStoreTreeUpdateByIdParamDTO item = new StoreJingdongHomeStoreTreeUpdateByIdParamDTO();
                item.setId(itemId);
                item.setNodePath(itemTargetNodePath);
                item.setNodeLevel(itemTargetNodeLevel);
                nextNodeUpdateList.add(item);
            }
            if(nextNodeUpdateList.size() > 0){
                storeJingdongHomeStoreTreeService.updateNextPathInfo(nextNodeUpdateList);
            }
        }

    }
}
